const personalCircleModel = require('../../model/personalCircleModel')

class PersonalCircleSer {
    // 查询
    async inquirePersonalCircleList(listQuery, whereData) {
        const data = await personalCircleModel.findAndCountAll({
            where: whereData,
            attributes:
                ['id',
                    ['circle_content', 'circleContent'],
                    ['circle_state', 'circleState'],
                    'createdAt'],
            limit: listQuery.limit,
            offset: listQuery.limit * (listQuery.page - 1),
        })
        return data
    }

    //按照日期降序查询所以
    async inquirePersonalCircleDateList(listQuery) {
        const data = await personalCircleModel.findAll({
            where: {
                circle_state: 1,
            },
            attributes:
                ['id',
                    ['circle_content', 'circleContent'],
                    ['circle_state', 'circleState'],
                    'createdAt'],
            limit: listQuery.limit,
            offset: listQuery.limit * (listQuery.page - 1),
            order: [['updatedAt', 'DESC']]
        })
        return data
    }

    // 添加
    async addPersonalCircle(json) {
        const data = await personalCircleModel.create(json)
        return data
    }

    // 删除
    async delPersonalCircle(id) {
        const data = await personalCircleModel.destroy({
            where: {
                id,
            },
        })
        return data
    }

    // 修改
    async updatePersonalCircle(text, id) {
        const data = await personalCircleModel.update(
            {
                circle_content: text
            },
            {
                where: {
                    id,
                },
            })
        return data
    }

    // 修改状态
    async updatePersonalCircleStatus(state, id) {
        const data = await personalCircleModel.update(
            {
                circle_state: state
            },
            {
                where: {
                    id,
                },
            })
        return data
    }

}

module.exports = new PersonalCircleSer()
